package com.huabo.monitor.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.JsonBean;
import com.huabo.monitor.entity.*;
import com.huabo.monitor.service.*;
import com.huabo.monitor.util.ConstClass;
import com.huabo.monitor.util.DateUtils;
import com.huabo.monitor.util.PageBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import springfox.documentation.spring.web.json.Json;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@Slf4j
@Api(value = "内控测试-测试方案", tags = {"内控测试-测试方案所有接口"})
@RequestMapping(value = "/nbkz")
public class CsfaController {

    @Resource
    ITblTestplanService testplanService;
    @Autowired
    ITblStaffService iTblStaffService;


    @Autowired
    TblAssessService tblAssessService;

    @Resource
    CsfaService csfaService;

    @Resource
    ITblTestelementService iTblTestelementService;

    @GetMapping(value = "/nkcs/plan/ctrltest_plan_list")
    @ApiOperation("测试方案-主页")
    public JsonBean nkcs_ctrltest_plan_list(
            @ApiParam(name = "pageNumber", value = "pageNumber", defaultValue = "1") @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
            @ApiParam(name = "plannumber", value = "plannumber") @RequestParam(value = "plannumber", required = false) String plannumber,
            @ApiParam(name = "planname", value = "planname") @RequestParam(value = "planname", required = false) String planname,
            @ApiParam(name = "starttime_min", value = "starttime_min") @RequestParam(value = "starttime_min", required = false) String starttime_min,
            @ApiParam(name = "starttime_max", value = "starttime_max") @RequestParam(value = "starttime_max", required = false) String starttime_max,
            @ApiParam(name = "status", value = "status") @RequestParam(value = "status", required = false) String status,
            @ApiParam(name = "year", value = "year") @RequestParam(value = "year", required = false) String year,

            @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token
    ) throws Exception {
        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }

        TblTestplan plan = new TblTestplan();
        if (planname != null && planname.length() > 0) {
            plan.setPlanname(planname);
        }
        if (plannumber != null && plannumber.length() > 0) {
            plan.setPlannumber(plannumber);
        }
        if (status != null && status.length() > 0) {
            plan.setPlanstatus(status);
        }
        if (year != null && year.length() > 0) {
            plan.setPlanyear(year);
        }

        TblStaffUtil user = DealUserToken.parseUserToken(token);
//        TblOrganization attribute = (TblOrganization) request.getSession().getAttribute("hbOrgEntity");// 选则的机构
//        TblStaff user = (TblStaff) request.getSession().getAttribute("longUser");

        boolean bool = csfaService.isSJByOrgId(user.getLinkDetp().getOrgid().toString());
        IPage<TblTestplan> iPage;
        if (bool) {
            iPage = csfaService.findAll(plan, pageNumber, starttime_min, starttime_max, user.getCurrentOrg().getOrgid());
        } else {
            iPage = csfaService.findAllnoSj(plan, pageNumber, starttime_min, starttime_max,
                    user.getStaffid().toString());
        }


        Map<String, Object> mv = new HashMap<>();
        mv.put("pageBean", iPage);
        mv.put("starttime_min", starttime_min);
        mv.put("starttime_max", starttime_max);
        mv.put("plan", plan);
        return new JsonBean(200, "success", mv);


    }


    @GetMapping(value = "/nkcs/plan/detail")
    @ApiOperation("测试方案-主页-点击编号/点击修改")
    public JsonBean nkcs_ctrltest_plan_detail(

            @ApiParam(name = "selectProjectid", value = "testplanid") @RequestParam(value = "selectProjectid") BigDecimal selectProjectid,
            @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token
    ) throws Exception {
        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }
        Map<String, Object> mv = new HashMap<>();
        mv.put("test", csfaService.queryOneTestPlanVo(selectProjectid));

        return new JsonBean(200, "success", mv);
    }

    @ApiOperation("测试方案-新建-得定制部门默认信息")
    @GetMapping(value = "/nkcs/plan/add")
    public JsonBean nkcs_ctrltest_plan_add(@ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token
    ) throws Exception {
        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }

        TblStaffUtil staff = DealUserToken.parseUserToken(token);
        TblOrganization organization = this.tblAssessService.queryOrganizationById(staff.getLinkDetp().getOrgid());
        Map<String, Object> mv = new HashMap<>();
        mv.put("org", organization);

        return new JsonBean(200, "success", mv);
    }


    /**
     * 通用获取设置的编号
     *
     * @param
     */
    @GetMapping(value = "/csfa/code/findAutoNumber")
    @ApiOperation("测试方案-新建-编号生成")
    public JsonBean findAutoNumber(HttpServletResponse response,
                                   @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token,
                                   @ApiParam(name = "tblName", value = "tblName", defaultValue = "TBL_TESTPLAN") @RequestParam(value = "tblName") String tblName,
                                   @ApiParam(name = "column", value = "column", defaultValue = "PLANNUMBER") @RequestParam(value = "column") String column,
                                   @ApiParam(name = "orgCol", value = "orgCol", defaultValue = "ORGID") @RequestParam(value = "orgCol") String orgCol,
                                   @ApiParam(name = "noId", value = "noId", defaultValue = "282") @RequestParam(value = "noId") Integer noId) throws Exception {
        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }
        TblStaffUtil staff = DealUserToken.parseUserToken(token);
        String flowNextId = null;
        try {
            flowNextId = tblAssessService.findFlowNextId(tblName, column, orgCol, staff.getCurrentOrg().getOrgid(), noId,
                    null, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.reset();
        return new JsonBean(200, "success", flowNextId);
    }

    @ApiOperation("测试方案/内控报告-新建/修改页面-选择评价负责人列表")
    @GetMapping(value = "/user/list")


    public JsonBean userListss(
            @ApiParam(name = "pageNumber", value = "pageNumber", defaultValue = "1") @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
            @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token,
            @ApiParam(name = "pid", value = "左侧树节点id.不传时查根", defaultValue = "198328") @RequestParam(value = "pid", required = false) String pid,
            @ApiParam(name = "realname", value = "用户真实名", defaultValue = "") @RequestParam(value = "realname", required = false) String realname
    ) throws Exception {
        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }
        TblStaffUtil staff = DealUserToken.parseUserToken(token);

        BigDecimal orgid = staff.getCurrentOrg().getOrgid();// 选则的机构
        TblOrganization tblOrganization;
        Integer orgtype;
        if (StringUtils.isNotBlank(pid)) {
            tblOrganization = this.tblAssessService.queryOrganizationById(new BigDecimal(pid));
            orgid = tblOrganization.getOrgid();
            orgtype = tblOrganization.getOrgtype().intValue();
        }else{
            tblOrganization = this.tblAssessService.queryOrganizationById(orgid);
            orgtype=tblOrganization.getOrgtype().intValue();
        }

        IPage<Map<String, Object>> iPage = new Page<>(pageNumber, ConstClass.DEFAULT_SIZE);

        this.tblAssessService.queryAllPageBeanPid(iPage, orgid, orgtype, realname);

        Map<String, Object> mv = new LinkedHashMap<>();
        mv.put("pid", pid);
        mv.put("pageBean", iPage);

        return new JsonBean(200, "success", mv);
    }


    /**
     * 内控测试方案选择模板
     *
     * @return
     */
    @ApiOperation("测试方案-新建/修改页面-选择测试模板分页列表(左侧机构树由前台负责)")
    @GetMapping(value = "/csfa/def_tmpl_list")
    public JsonBean csfa_def_tmpl_list(
            @ApiParam(name = "pageNumber", value = "pageNumber", defaultValue = "1") @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,

            @ApiParam(name = "templeNumber", value = "模板编号") @RequestParam(value = "templeNumber", required = false) String templeNumber,
            @ApiParam(name = "templename", value = "模板名称") @RequestParam(value = "templename", required = false) String templename,
            @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token
    ) throws Exception {
        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }
        ModelAndView view = new ModelAndView();
        TblStaffUtil staff = DealUserToken.parseUserToken(token);
        IPage<TblTesttempleVo> ipage = this.csfaService.getTestTemp(staff.getCurrentOrg().getOrgid(), pageNumber,
                templeNumber, templename);

        Map<String, Object> mv = new LinkedHashMap<>();
        mv.put("templename", templename);
        mv.put("templeNumber", templeNumber);
        mv.put("pageBean", ipage);
        return new JsonBean(200, "success", mv);

    }

    @ApiOperation("测试方案-添加页面-保存")
    @PostMapping(value = "/nkcs/plan/save")
    public JsonBean nkcs_ctrltest_plan_save(
            @ApiParam(name = "plannumber", value = "计划编号") @RequestParam(value = "plannumber", required = false) String plannumber,
            @ApiParam(name = "planname", value = "计划名称") @RequestParam(value = "planname", required = false) String planname,
            @ApiParam(name = "planyear", value = "计划年度") @RequestParam(value = "planyear", required = false) String planyear,

            @ApiParam(name = "testtype", value = "测试类型") @RequestParam(value = "testtype", required = false) String testtype,
            @ApiParam(name = "oid2", value = "计划制定部门id") @RequestParam(value = "oid2", required = false) String oid,
            @ApiParam(name = "org2", value = "计划制定部门名称") @RequestParam(value = "org2", required = false) String org2,

            @ApiParam(name = "starttime_min", value = "计划时间min") @RequestParam(value = "starttime_min", required = false) String starttime_min,
            @ApiParam(name = "starttime_max", value = "计划时间max") @RequestParam(value = "starttime_max", required = false) String starttime_max,
            @ApiParam(name = "staffid", value = "负责人id") @RequestParam(value = "staffid", required = false) String staffid,
            @ApiParam(name = "staffname", value = "负责人名") @RequestParam(value = "staffname", required = false) String staffname,

            @ApiParam(name = "cost", value = "开展费用") @RequestParam(value = "cost", required = false) String cost,
            @ApiParam(name = "people", value = "投入人力") @RequestParam(value = "people", required = false) String people,
            @ApiParam(name = "org3", value = "被测试机构名称") @RequestParam(value = "org3", required = false) String org3,
            @ApiParam(name = "testtemid", value = "测试模板id") @RequestParam(value = "testtemid", required = false) String testtemid,

            @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token
    ) throws Exception {
        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }
        TblStaffUtil staff = DealUserToken.parseUserToken(token);
        TblTestplan plan = new TblTestplan();

        plan.setCreatid(staff.getStaffid());
        plan.setPlanname(planname);

        plan.setPlanyear(planyear);
        plan.setTesttype(testtype);
        plan.setTestedorgs(org3);
        plan.setPlanmadedep(org2);
//
//
        QueryWrapper<TblTestelement> wrapper = new QueryWrapper<>();
        wrapper.eq("templid", new BigDecimal(testtemid));
        final long count = iTblTestelementService.count(wrapper);

        if (count <= 0) {
            return new JsonBean(-1, "模板无内容，禁止使用,请重新选择", null);
        }

        if (StringUtils.isNotBlank(oid)) {
            plan.setOrgid(new BigDecimal(oid));
        }

        if (StringUtils.isNotBlank(cost)) {
            plan.setPlanfee(new BigDecimal(cost));
        }

        if (StringUtils.isNotBlank(staffid)) {
            plan.setStaffid(new BigDecimal(staffid));
        }

        plan.setPlanstatus("未启动");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {

            if (StringUtils.isNotBlank(starttime_min)) {
                plan.setStarttime(DateUtils.dateToLocalDateTime(formatter.parse(starttime_min)));
            }

            if (StringUtils.isNotBlank(starttime_max)) {
                plan.setEndtime(DateUtils.dateToLocalDateTime(formatter.parse(starttime_max)));
            }

        } catch (ParseException e) {
            e.printStackTrace();
            return new JsonBean(-1, "日期格式不正确,保存失败", null);
        }
        plan.setPlanleader(staffname);
        plan.setNumberofpeople(people);
        plan.setPlannumber(plannumber);

        if (StringUtils.isNotBlank(testtemid)) {


            plan.setTesttemid(new BigDecimal(testtemid));
        }

        this.testplanService.save(plan);
        this.csfaService.delTesttasksByPanId(plan.getTestplanid());

        return new JsonBean(200, "添加成功", plan);
    }

    @ApiOperation("测试方案-修改页面-修改保存")
    @PostMapping(value = "/nkcs/plan/modify_save")
    public JsonBean nkcs_ctrltest_plan_modify_save(
            @ApiParam(name = "selectProjectid", value = "testplanid") @RequestParam(value = "selectProjectid") BigDecimal selectProjectid,
            @ApiParam(name = "plannumber", value = "计划编号") @RequestParam(value = "plannumber", required = false) String plannumber,
            @ApiParam(name = "planname", value = "计划名称") @RequestParam(value = "planname", required = false) String planname,
            @ApiParam(name = "planyear", value = "计划年度") @RequestParam(value = "planyear", required = false) String planyear,

            @ApiParam(name = "testtype", value = "测试类型") @RequestParam(value = "testtype", required = false) String testtype,
            @ApiParam(name = "oid2", value = "计划制定部门id") @RequestParam(value = "oid2", required = false) String oid,
            @ApiParam(name = "org2", value = "计划制定部门名称") @RequestParam(value = "org2", required = false) String org2,

            @ApiParam(name = "starttime_min", value = "计划时间min") @RequestParam(value = "starttime_min", required = false) String starttime_min,
            @ApiParam(name = "starttime_max", value = "计划时间max") @RequestParam(value = "starttime_max", required = false) String starttime_max,
            @ApiParam(name = "staffid", value = "负责人id") @RequestParam(value = "staffid", required = false) String staffid,
            @ApiParam(name = "staffname", value = "负责人名") @RequestParam(value = "staffname", required = false) String staffname,

            @ApiParam(name = "cost", value = "开展费用") @RequestParam(value = "cost", required = false) String cost,
            @ApiParam(name = "people", value = "投入人力") @RequestParam(value = "people", required = false) String people,
            @ApiParam(name = "org3", value = "被测试机构名称") @RequestParam(value = "org3", required = false) String org3,
            @ApiParam(name = "testtemid", value = "测试模板id") @RequestParam(value = "testtemid", required = false) String testtemid,

            @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token
    ) throws Exception {
        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }
        TblStaffUtil staff = DealUserToken.parseUserToken(token);
        TblTestplan plan = this.testplanService.getById(selectProjectid);
        if (plan == null) {
            return new JsonBean(-1, "所传id值不正确,无此数据", selectProjectid);
        }

        plan.setCreatid(staff.getStaffid());
        plan.setPlanname(planname);

        plan.setPlanyear(planyear);
        plan.setTesttype(testtype);
        plan.setTestedorgs(org3);
        plan.setPlanmadedep(org2);
//
//
        QueryWrapper<TblTestelement> wrapper = new QueryWrapper<>();
        wrapper.eq("templid", new BigDecimal(testtemid));
        final long count = iTblTestelementService.count(wrapper);

        if (count <= 0) {
            return new JsonBean(-1, "模板无内容，禁止使用,请重新选择", null);
        }

        if (StringUtils.isNotBlank(oid)) {
            plan.setOrgid(new BigDecimal(oid));
        }

        if (StringUtils.isNotBlank(cost)) {
            plan.setPlanfee(new BigDecimal(cost));
        }

        if (StringUtils.isNotBlank(staffid)) {
            plan.setStaffid(new BigDecimal(staffid));
        }

        plan.setPlanstatus("未启动");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {

            if (StringUtils.isNotBlank(starttime_min)) {
                plan.setStarttime(DateUtils.dateToLocalDateTime(formatter.parse(starttime_min)));
            }

            if (StringUtils.isNotBlank(starttime_max)) {
                plan.setEndtime(DateUtils.dateToLocalDateTime(formatter.parse(starttime_max)));
            }

        } catch (ParseException e) {
            e.printStackTrace();
            return new JsonBean(-1, "日期格式不正确,保存失败", null);
        }
        plan.setPlanleader(staffname);
        plan.setNumberofpeople(people);
        plan.setPlannumber(plannumber);

        if (StringUtils.isNotBlank(testtemid)) {


            plan.setTesttemid(new BigDecimal(testtemid));
        }

        this.testplanService.updateById(plan);
        // 级联删除,添加testtask操作
        this.csfaService.delTesttasksByPanId(plan.getTestplanid());


        return new JsonBean(200, "修改成功", plan);
    }




    @GetMapping(value = "/nkcs/plan/isStatus", produces = "application/json; charset=utf-8")
    @ApiOperation("测试方案-点击分配回传状态")
    public @ResponseBody
    String zgfp_fqStatus(
            @ApiParam(name = "selectProjectid", value = "testplanid") @RequestParam(value = "selectProjectid") BigDecimal selectProjectid,
                         @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token
    ) {
        if (!ConstClass.checkToken(token)) {
            return JsonBean.error("缺少token或不正确");
        }

        if (selectProjectid != null) {

            TblTestplan plan = this.testplanService.getById(selectProjectid);
            if (plan != null && plan.getPlanstatus().equals("未启动")) {
                return JsonBean.success();
            } else {
                if (plan != null && plan.getPlanstatus().equals("完成")) {
                    return JsonBean.error("该方案已完成");
                } else {
                    return JsonBean.error("该方案已启动");
                }
            }
        } else {
            return JsonBean.error("请选择");
        }
    }

    @ApiOperation("测试方案-分配-左侧树")
    @GetMapping(value = "/csfa/gettree")
    public JsonBean csfa_getTree(
            @ApiParam(name = "planid", value = "testplanid") @RequestParam(value = "planid")  String planid,

            @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token
    ) {
        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }


        Map<String, Object> mv = new LinkedHashMap<>();
        mv.put("planid", planid);

        if (StringUtils.isNotBlank(planid)) {
            TblTestplan plan = this.testplanService.getById(new BigDecimal(planid));

            if (plan != null) {
                mv.put("templId", plan.getTesttemid());
                List<Tree> tree = this.csfaService.getTreeC(plan.getTesttemid());
                mv.put("tree", tree);

            }
        }

        return new JsonBean(200, "success", mv);
    }


    @ApiOperation("测试方案-分配-右侧列表")
    @GetMapping(value = "/csfa/def_list")
    public JsonBean csfa_gzdg_def_list(
            @ApiParam(name = "pageNumber", value = "pageNumber", defaultValue = "1") @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
            @ApiParam(name = "node", value = "左侧节点id") @RequestParam(value = "node",required=false) BigDecimal node,
            @ApiParam(name = "templId", value = "templId") @RequestParam(value = "templId",required=false) BigDecimal templId,
            @ApiParam(name = "planid", value = "planid") @RequestParam(value = "planid",required=false)BigDecimal planid,
            @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token
    ) {
        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }
//

        IPage<Map<String,Object>> ipage=null;
        if (templId != null && node != null) {
            ipage = this.csfaService.fingByTree(node, templId, planid, pageNumber);
        }
        Map<String, Object> mv = new LinkedHashMap<>();
        mv.put("node", node);
        mv.put("templId", templId);
        mv.put("planid", planid);
        mv.put("pageBean", ipage);

        return new JsonBean(200, "success", mv);
    }


    @ApiOperation("测试方案-分配-设置人员右侧列表(左树由前台完成)")
    @GetMapping(value = "/csfa/list")
    public JsonBean userListss2(
            @ApiParam(name = "pageNumber", value = "pageNumber", defaultValue = "1") @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
            @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token,
            @ApiParam(name = "pid", value = "左侧树节点id.不传时查根", defaultValue = "") @RequestParam(value = "pid", required = false) String pid,
            @ApiParam(name = "realname", value = "用户真实名模糊查询用", defaultValue = "") @RequestParam(value = "realname", required = false) String realname,
            @ApiParam(name = "node", value = "之前的node值回传用") @RequestParam(value = "node",required=false) BigDecimal node,
            @ApiParam(name = "templId", value = "之前的templId回传用") @RequestParam(value = "templId",required=false) BigDecimal templId,
            @ApiParam(name = "planid", value = "之前的planid回传用") @RequestParam(value = "planid",required=false)BigDecimal planid,
            @ApiParam(name = "task", value = "ELEMENTID多值用,隔开来自checkbox比如226760,226761回传用") @RequestParam(value = "task",required=false)String task

    ) throws Exception {
        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }
        TblStaffUtil staff = DealUserToken.parseUserToken(token);

        BigDecimal orgid = staff.getCurrentOrg().getOrgid();// 选则的机构
        TblOrganization tblOrganization;
        Integer orgtype;
        if (StringUtils.isNotBlank(pid)) {
            tblOrganization = this.tblAssessService.queryOrganizationById(new BigDecimal(pid));
            orgid = tblOrganization.getOrgid();
            orgtype = tblOrganization.getOrgtype().intValue();
        }else{
             tblOrganization = this.tblAssessService.queryOrganizationById(orgid);
             orgtype=tblOrganization.getOrgtype().intValue();
        }

        IPage<Map<String, Object>> iPage = new Page<>(pageNumber, ConstClass.DEFAULT_SIZE);

        this.tblAssessService.queryAllPageBeanPid(iPage, orgid, orgtype, realname);

        Map<String, Object> mv = new LinkedHashMap<>();
        mv.put("pid", pid);
        mv.put("realname", realname);
        mv.put("node", node);
        mv.put("templId", templId);
        mv.put("planid", planid);
        mv.put("task", task);
        mv.put("pageBean", iPage);

        return new JsonBean(200, "success", mv);
    }

    @ApiOperation("测试方案-分配-设置人员-选定")
    @PostMapping(value = "/csfa/saveUser")
    public JsonBean start_isStatus(
            @ApiParam(name = "task", value = "ELEMENTID多值用,隔开来自checkbox比如226760,226761回传用") @RequestParam(value = "task")String task,
            @ApiParam(name = "planid", value = "planid") @RequestParam(value = "planid")String planid,
            @ApiParam(name = "userid", value = "userid") @RequestParam(value = "userid")String userid,
            @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token

    ) {
        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }
        Map<String, Object> mv = new LinkedHashMap<>();
        if (StringUtils.isNotBlank(task) && StringUtils.isNotBlank(planid)) {
            this.csfaService.updateTesttsak(task,planid,userid);

        } else {
            return new JsonBean(-1, "请选择",null);
        }
        mv.put("task",task);
        mv.put("planid",planid);
        mv.put("userid",userid);
        return new JsonBean(200, "success", mv);
    }



    /**
     * 启动测试方案
     *
     * @param
     * @return
     */
    @PostMapping(value = "/nkcs/plan/start_isStatus", produces = "application/json; charset=utf-8")
    @ApiOperation("测试方案-启动")
    public
    JsonBean start_isStatus(
       @ApiParam(name = "selectProjectid", value = "testplanid") @RequestParam(value = "selectProjectid") BigDecimal selectProjectid,
       @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token
    ) {
        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }

        if (selectProjectid != null) {

            TblTestplan plan = this.testplanService.getById(selectProjectid);
            Integer wcCount =  this.csfaService.findByPlan(selectProjectid);
            Integer wCount =   this.csfaService.findByPlanwStra(selectProjectid);
            if (plan != null && plan.getPlanstatus().equals("未启动") && wcCount != null && wCount != null
                    && wcCount.toString().equals(wCount.toString())) {
                  plan.setPlanstatus("已启动");
                  testplanService.updateById(plan);
                  return new JsonBean(200, "success", plan);
            } else {
                if (plan != null && plan.getPlanstatus().equals("完成")) {
                    return new JsonBean(-1, "该方案已完成", plan);

                } else if (wCount == null || !wcCount.toString().equals(wCount.toString())) {
                    return new JsonBean(-1, "该方案未完成分配", plan);

                } else {
                    return new JsonBean(-1, "该方案已启动", plan);
                }
            }
        } else {
            return new JsonBean(-1, "请选择",null);
        }
    }


    @ApiOperation("测试方案-删除")
    @PostMapping(value = "/nkcs/plan/delete")
    public JsonBean nkcs_ctrltest_plan_delete(
            @ApiParam(name = "selectProjectid", value = "testplanid") @RequestParam(value = "selectProjectid") BigDecimal selectProjectid,
            @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token
    ) {
        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }

        if (selectProjectid != null) {
            this.csfaService.deleteTestPlan(selectProjectid);

        }
        return new JsonBean(200, "success", null);
    }






}
